package hibu.LeiJonGun.seniorservice;

import java.util.ArrayList;
import java.util.HashMap;

import android.util.SparseArray;

/*
 * Written by Christian Gunvaldsen, Henrik Leinaas and Elis Tor Jonsson
 * 
 * Stores data of the user
*/
public class User {
	private SparseArray<Assignment> assignmentList = new SparseArray<Assignment>();
	private HashMap<String,User> contacts = new HashMap<String,User>(); // Accepted contacts
	private HashMap<String,User> contactsRecived = new HashMap<String,User>(); // Contact request received
	private HashMap<String,User> contactsSent = new HashMap<String,User>(); // Contact request sent
	private String firstname;
	private roleType role;
	private String surname;
	private String username;
	
	//Housekeeping
	public User(String username,String firstname, String surname, roleType role) throws Exception {
		//Data check:
		if (username==null || username=="" || username.length() <4) throw new Exception("Fill in username");
		if (firstname==null || firstname==""|| firstname.length() < 2) throw new Exception("Fill in firstname");
		if (surname==null || surname==""|| surname.length()< 2) throw new Exception("Fill in surname");
		if (role==null) throw new Exception("Choose rolltype");
		
		//OK:
		this.username =username;
		this.firstname=firstname;
		this.surname=surname;
		this.role=role;
	}
	
	public void addAssignment(Assignment assignment) throws Exception {
		if (assignment == null) throw new Exception("Assignment cant be empty");
		assignmentList.put(assignment.getAssignmentID(), assignment);
	}
	
	public void addContact(User contact) throws Exception {
		if (contact == null) throw new Exception("Empty contact");
		contacts.put(contact.getUsername(), contact);
	}
	
	public void addContactRequestRecived(User contact) throws Exception {
		if (contact == null) throw new Exception("Empty contact");
		contactsRecived.put(contact.getUsername(), contact);
	}
	
	public void addContactRequestSent(User contact) throws Exception {
		if (contact == null) throw new Exception("Empty contact");
		contactsSent.put(contact.username, contact);
	}
	
	public void delAssignment(int assignmentID) throws Exception {
		if (assignmentList.get(assignmentID) == null) throw new Exception("Assignment not found");
		assignmentList.remove(assignmentID);
	}
	
	public void delContact(String username) throws Exception {
		if (contacts.get(username) == null) throw new Exception("User not found");
		contacts.remove(username);
	}
	
	public void delContactRequestRecieved(String username) throws Exception {
		if (contactsRecived.get(username) == null) throw new Exception("User not found");
		contactsRecived.remove(username);
	}
	
	public void delContactRequestSent(String username) throws Exception {
		if (contactsSent.get(username) == null) throw new Exception("User not found");
		contactsSent.remove(username);
	}
	
	public Assignment findAssignment(int assignmentID) {
		return assignmentList.get(assignmentID);
	}	
	
	public User findContact(String username) {
		return contacts.get(username);
	}
	
	public User findContactRequestRecieved(String username) {
		return contactsRecived.get(username);
	}
	
	public User findContactRequestSent(String username) {
		return contactsSent.get(username);
	}

	public SparseArray<Assignment> getAssignments() {return assignmentList;}
	
	public String getAssignmentStatus(int assignmentID) {
		if (assignmentList.get(assignmentID).isFree()) return "free";
		else if (assignmentList.get(assignmentID).isTakenAndNotFinished()) return "taken";
		else return "finished";
	}
	
	// Is used to compare contact requests with actual requests stored in DB.. for updates:
	public String getContactRecivedUsernames() { 
		String returnString = "";
		for (User u : contactsRecived.values()) {
			returnString += u.getUsername() + "@";
		}
		if (returnString.length() >0) return returnString.substring(0,returnString.length()-1);
		else return "";
	}
		
	public ArrayList<User> getContactRequestList() {return new ArrayList<User>(contactsRecived.values());}
	
	public ArrayList<User> getContacts() {return new ArrayList<User>(contacts.values());}
	
	// Is used to compare contacts with actual contacts stored in DB.. for updates:
	public String getContactUsernames()  { 
		String returnString = "";
		for (User u : contacts.values()) {
			returnString += u.getUsername() + "@";
		}
		if (returnString.length() >0) return returnString.substring(0,returnString.length()-1);
		else return "";
	}
		
	public String getCustomViewUserText() {
		return firstname + " " + surname + " (" + username + ")";
	}	
		
	public String getFirstname() {return firstname;}
	
	public String getFirstnameAndUsername() {return firstname + "(" + username + ")";}
	
	public String getFullname() {return firstname + " "+ surname;}
	
	// Is used to compare assignments with data stored in database
	public String getLocalAssignments() {
		String returnString = "";
		for (int i = 0; i < assignmentList.size(); i++) {
			returnString += assignmentList.valueAt(i).getAssignmentID() + ";" + 
					getAssignmentStatus(assignmentList.valueAt(i).getAssignmentID()) +"@";
		}
		if (returnString.length() >0) return returnString.substring(0,returnString.length()-1);
		else return "";
	}
		
	// Is used to compare contacts and request with data in database
	public String getLocalContacts() {return (getContactUsernames()+ "#" +getContactRecivedUsernames());}	
		
	public roleType getRoleType() {return role;}	
	
	public String getSurname() {return surname;}
		
	public String getUsername() {return username;}

}
